﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using InfFront.UICore;

namespace InfFront.InfoSources.Base
{
    public class GetIntegralFieldValueEventArgs : EventArgs
    {
        public object Object { get; set; }
        public object Value  { get; set; }
    }

    public class SetIntegralFieldValueEventArgs : EventArgs
    {
        public object Object { get; set; }
        public object Value  { get; set; }
    }

    public delegate void GetIntegralFieldValueHandler(object sender, GetIntegralFieldValueEventArgs args);
    public delegate void SetIntegralFieldValueHandler(object sender, SetIntegralFieldValueEventArgs args);

    public class IntegralInfoField : InfoField, IInfoField
    {
        #region Initialization/Construction
        public IntegralInfoField()
        {
            IsReadOnly = true;
        }

        public IntegralInfoField(IContainer container)
            : base(container)
        {
            container.Add(this);

            IsReadOnly = true;
        }
        #endregion Initialization/Construction

        private static readonly object EVENT_GET_FIELD = new object();

        public event GetIntegralFieldValueHandler OnGetValue
        {
            add { Events.AddHandler(EVENT_GET_FIELD, value); }
            remove { Events.RemoveHandler(EVENT_GET_FIELD, value); }
        }

        private static readonly object EVENT_SET_FIELD = new object();

        public event SetIntegralFieldValueHandler OnSetValue
        {
            add
            {
                Events.AddHandler(EVENT_SET_FIELD, value); 
                IsReadOnly = false;
            }
            remove { Events.RemoveHandler(EVENT_SET_FIELD, value); }
        }

        public override object GetValue(object obj)
        {
            var handler = (GetIntegralFieldValueHandler)Events[EVENT_GET_FIELD];
            var args = new GetIntegralFieldValueEventArgs() { Object = obj };
            if (handler != null)
                handler(this, args);
            return args.Value;
        }

        public override void SetValue(object obj, object value)
        {
            var handler = (SetIntegralFieldValueHandler)Events[EVENT_SET_FIELD];
            if (handler != null)
            {
                var args = new SetIntegralFieldValueEventArgs() { Object = obj, Value = value };
                handler(this, args);
            }
        }

        [Category("General"), DefaultValue(true), Description("Is the field for read only")]
        public new bool IsReadOnly
        {
            get { return base.IsReadOnly; }
            set { base.IsReadOnly = value; }
        }

        bool IInfoField.GetIsReadOnly()
        {
            if (IsReadOnly)
                return true;
            var handler = (SetIntegralFieldValueHandler)Events[EVENT_SET_FIELD];
            if (handler == null)
                return true;
            return false;
        }
    }
}
